//
//  LVResetPasswordViewController.m
//  LEVE
//
//  Created by 王若聪 on 2017/2/18.
//  Copyright © 2017年 dashuju. All rights reserved.
//

#import "LVResetPasswordViewController.h"

@interface LVResetPasswordViewController ()
@property (nonatomic, strong)UIButton    *completeBtn;

@property (nonatomic, strong)UILabel      *titleLabel;
@property (nonatomic, strong)UILabel      *descriptionLabel;

@property (nonatomic, strong)UILabel      *leveNameLabel;
@property (nonatomic, strong)UILabel      *currentLeveNameLabel;

@property (nonatomic, strong)UILabel      *passwordLabel;
@property (nonatomic, strong)UITextField  *passwordTextField;

@property (nonatomic, strong)UILabel      *confirmPasswordLabel;
@property (nonatomic, strong)UITextField  *confirmPasswordTextField;

@property (nonatomic, strong)UIButton    *passwordTextFieldClearTextBtn;
@property (nonatomic, strong)UIButton    *confirmPasswordTextFieldClearTextBtn;
@end

@implementation LVResetPasswordViewController

- (instancetype)init{
    self = [super init];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldDidChange:) name:UITextFieldTextDidChangeNotification object:nil];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];

    UIButton *completeBtn = [[UIButton alloc]init];
    completeBtn.size = CGSizeMake(37, 25);
    [completeBtn setTitle:@"完成" forState:UIControlStateNormal];
    [completeBtn setTitleColor:UIColorFromRGB(0x006FFF) forState:UIControlStateNormal];
    [completeBtn addTarget:self action:@selector(completeBtnTouched) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *completeItem = [[UIBarButtonItem alloc]initWithCustomView:completeBtn];
    self.navigationItem.rightBarButtonItem = completeItem;

    
    self.titleLabel = [[UILabel alloc]init];
    self.titleLabel.text = @"设置乐无密码";
    self.titleLabel.font = [UIFont boldSystemFontOfSize:18];
    self.titleLabel.textColor  = UIColorFromRGB(0x4A4A4A);
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:self.titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        NSNumber *topValue = kScreenWidth > 320 ? @98 : @58;

        make.top.equalTo(topValue);
        make.height.equalTo(@20);
        make.width.equalTo(self.view.mas_width);
    }];
    
    self.descriptionLabel = [[UILabel alloc]init];
    NSString *currentUserPhoneNum = @"+86 17620371033";
    self.descriptionLabel.text = [NSString stringWithFormat:@"用户密码设置成功之后，你可以使用手机号%@与乐无密码进行登录。",currentUserPhoneNum];
    self.descriptionLabel.numberOfLines = 0;
    self.descriptionLabel.font = [UIFont systemFontOfSize:14];
    self.descriptionLabel.textColor  = UIColorFromRGB(0x9B9B9B);
    
    [self.view addSubview:self.descriptionLabel];
    [self.descriptionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(30);
        
        NSNumber *paddingValue = kScreenWidth > 320 ? @50 : @20;

        make.left.equalTo(paddingValue);
        make.right.equalTo(self.view.mas_right).offset(-paddingValue.intValue);
        make.width.lessThanOrEqualTo(self.view.mas_width);
    }];
    
    self.leveNameLabel = [[UILabel alloc]init];
    self.leveNameLabel.text = @"乐无号";
    self.leveNameLabel.font = [UIFont systemFontOfSize:18];
    self.leveNameLabel.textColor  = UIColorFromRGB(0x9B9B9B);
    [self.view addSubview:self.leveNameLabel];
    [self.leveNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        NSNumber *topValue = kScreenWidth > 320 ? @65 : @35;

        
        make.top.equalTo(self.descriptionLabel.mas_bottom).offset(topValue.intValue);
        make.left.equalTo(self.descriptionLabel.mas_left);
        make.height.equalTo(@25);
        make.width.equalTo(@60);
    }];
    
    self.currentLeveNameLabel = [[UILabel alloc]init];
    NSString *currentLeveName = @"Rambo0718";
    self.currentLeveNameLabel.text = currentLeveName;
    self.currentLeveNameLabel.font = [UIFont systemFontOfSize:18];
    self.currentLeveNameLabel.textColor  = UIColorFromRGB(0x9B9B9B);
    [self.view addSubview:self.currentLeveNameLabel];
    [self.currentLeveNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.leveNameLabel.mas_top).offset(2);
        make.left.equalTo(self.leveNameLabel.mas_right).offset(50);
        make.right.equalTo(self.descriptionLabel.mas_right);
    }];
    
    UIView *bottomLineView = [[UIView alloc]init];
    bottomLineView.backgroundColor = UIColorFromRGB(0xD7D7D7);
    [self.view addSubview:bottomLineView];
    [bottomLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.leveNameLabel.mas_bottom).offset(8);
        make.height.equalTo(@2);
        make.left.equalTo(self.leveNameLabel.mas_left);
        make.right.equalTo(self.currentLeveNameLabel.mas_right);
    }];
    
    self.passwordLabel = [[UILabel alloc]init];
    self.passwordLabel.text = @"密码";
    self.passwordLabel.font = [UIFont systemFontOfSize:18];
    self.passwordLabel.textColor  = UIColorFromRGB(0x4A4A4A);
    [self.view addSubview:self.passwordLabel];
    [self.passwordLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.leveNameLabel.mas_bottom).offset(35);
        make.left.equalTo(self.descriptionLabel.mas_left);
        make.height.equalTo(@25);
        make.width.equalTo(@60);
    }];
    
    self.passwordTextField = [[UITextField alloc]init];
    self.passwordTextField.placeholder = @"请设置乐无密码";
    self.passwordTextField.tintColor = UIColorFromRGB(0x4A4A4A);
    self.passwordTextField.secureTextEntry = YES;
    [self.view addSubview:self.passwordTextField];
    [self.passwordTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@20);
        make.left.equalTo(self.currentLeveNameLabel.mas_left);
        make.top.equalTo(self.passwordLabel.mas_top).offset(2);
        make.right.equalTo(self.currentLeveNameLabel.mas_right);
    }];
    [self.passwordTextField setValue:[UIFont systemFontOfSize:18] forKeyPath:@"_placeholderLabel.font"];
    [self.passwordTextField setValue:UIColorFromRGBA(0xA1A7AE, .6f) forKeyPath:@"_placeholderLabel.textColor"];
    
    UIView *bottomLineView2 = [[UIView alloc]init];
    bottomLineView2.backgroundColor = UIColorFromRGB(0xD7D7D7);
    [self.view addSubview:bottomLineView2];
    [bottomLineView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.passwordLabel.mas_bottom).offset(8);
        make.height.equalTo(@2);
        make.left.equalTo(self.passwordLabel.mas_left);
        make.right.equalTo(self.passwordTextField.mas_right);
    }];
    
    self.confirmPasswordLabel = [[UILabel alloc]init];
    self.confirmPasswordLabel.text = @"确认密码";
    self.confirmPasswordLabel.font = [UIFont systemFontOfSize:18];
    self.confirmPasswordLabel.textColor  = UIColorFromRGB(0x4A4A4A);
    [self.view addSubview:self.confirmPasswordLabel];
    [self.confirmPasswordLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.passwordLabel.mas_bottom).offset(35);
        make.left.equalTo(self.passwordLabel.mas_left);
        make.height.equalTo(@25);
        make.width.equalTo(@75);
    }];
    
    self.confirmPasswordTextField = [[UITextField alloc]init];
    self.confirmPasswordTextField.placeholder = @"请再次输入";
    self.confirmPasswordTextField.tintColor = UIColorFromRGB(0x4A4A4A);
    self.confirmPasswordTextField.secureTextEntry = YES;
    [self.view addSubview:self.confirmPasswordTextField];
    [self.confirmPasswordTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@20);
        make.left.equalTo(self.passwordTextField.mas_left);
        make.top.equalTo(self.confirmPasswordLabel.mas_top).offset(2);
        make.right.equalTo(self.passwordTextField.mas_right);
    }];
    [self.confirmPasswordTextField setValue:[UIFont systemFontOfSize:18] forKeyPath:@"_placeholderLabel.font"];
    [self.confirmPasswordTextField setValue:UIColorFromRGBA(0xA1A7AE, .6f) forKeyPath:@"_placeholderLabel.textColor"];
    
    UIView *bottomLineView3 = [[UIView alloc]init];
    bottomLineView3.backgroundColor = UIColorFromRGB(0xD7D7D7);
    [self.view addSubview:bottomLineView3];
    [bottomLineView3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.confirmPasswordLabel.mas_bottom).offset(8);
        make.height.equalTo(@2);
        make.left.equalTo(self.confirmPasswordLabel.mas_left);
        make.right.equalTo(self.confirmPasswordTextField.mas_right);
    }];
    

    
    self.passwordTextFieldClearTextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.passwordTextFieldClearTextBtn setImage:[UIImage imageNamed:@"btn_delete_default"] forState:UIControlStateNormal];
    [self.passwordTextFieldClearTextBtn setImage:[UIImage imageNamed:@"btn_delete_selected"] forState:UIControlStateNormal];
    [self.passwordTextFieldClearTextBtn addTarget:self action:@selector(passwordTextFieldClearTextBtnTouched) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.passwordTextFieldClearTextBtn];
    [self.passwordTextFieldClearTextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@16);
        make.width.equalTo(@16);
        make.left.equalTo(self.passwordTextField.mas_right).offset(-16);
        make.right.equalTo(self.passwordTextField.mas_right);
        make.bottom.equalTo(self.passwordTextField.mas_bottom);
    }];
    self.passwordTextFieldClearTextBtn.hidden = YES;
    
    self.confirmPasswordTextFieldClearTextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.confirmPasswordTextFieldClearTextBtn setImage:[UIImage imageNamed:@"btn_delete_default"] forState:UIControlStateNormal];
    [self.confirmPasswordTextFieldClearTextBtn setImage:[UIImage imageNamed:@"btn_delete_selected"] forState:UIControlStateNormal];
    [self.confirmPasswordTextFieldClearTextBtn addTarget:self action:@selector(confirmPasswordTextFieldClearTextBtnTouched) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.confirmPasswordTextFieldClearTextBtn];
    [self.confirmPasswordTextFieldClearTextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@16);
        make.width.equalTo(@16);
        make.left.equalTo(self.confirmPasswordTextField.mas_right).offset(-16);
        make.right.equalTo(self.confirmPasswordTextField.mas_right);
        make.bottom.equalTo(self.confirmPasswordTextField.mas_bottom);
    }];
    self.confirmPasswordTextFieldClearTextBtn.hidden = YES;
    
}
- (void)passwordTextFieldClearTextBtnTouched{
    self.passwordTextField.text = nil;
}

- (void)confirmPasswordTextFieldClearTextBtnTouched{
    self.confirmPasswordTextField.text = nil;
}

- (void)completeBtnTouched{
    

}

#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if ([string isEqualToString:@"\n"]) {
        [self completeBtnTouched];
        return NO;
    }
    return YES;
}

- (void)textFieldDidChange:(NSNotification*)notification{
    
    if ([self.passwordTextField.text length]) {
        self.passwordTextFieldClearTextBtn.hidden = NO;
    }else{
        self.passwordTextFieldClearTextBtn.hidden = YES;
    }
    
    if ([self.confirmPasswordTextField.text length]) {
        self.confirmPasswordTextFieldClearTextBtn.hidden = NO;
    }else{
        self.confirmPasswordTextFieldClearTextBtn.hidden = YES;
    }
    
}

- (void)textFieldDidBeginEditing:(UITextField *)textField{

    
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [super touchesBegan:touches withEvent:event];
    [self.passwordTextField resignFirstResponder];
    self.passwordTextFieldClearTextBtn.hidden = YES;
    [self.confirmPasswordTextField resignFirstResponder];
    self.confirmPasswordTextFieldClearTextBtn.hidden = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
